<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
       <el-form-item label="用户名称" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          :disabled = "createVisible"
        />
      </el-form-item>
      <el-form-item label="问题类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择问题类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否回复" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择是否回复" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item style="color:red">
        <label>      当前未回复条数：{{this.undototal}}</label>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:consult:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:consult:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consultList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="是否回复" align="center" prop="status" :formatter="statusFormat" >
        <template scope="scope">
        <span v-if="scope.row.status=='0'" style="color: red">{{statusFormat(scope.row)}}</span>
        <span v-else style="color: green">{{statusFormat(scope.row)}}</span>
    </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="提问者" align="center" prop="customerName" />
      <el-table-column label="问题描述" align="center" prop="question" />
      <el-table-column label="问题回复" align="center" prop="reply" />
      <el-table-column label="联系方式" align="center" prop="tel" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-news"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:consult:edit']"
          >回复</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status==0"
            v-hasPermi="['business:consult:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 回复对话框 -->
    <el-dialog title="回复" :visible.sync="replyOpen" width="500px" append-to-body >
      <el-form ref="forn" :model="forn" label-width="80px" :rules="rulesr">
        <el-form-item label="回复" prop="reply" >
          <el-input v-model="forn.reply" placeholder="在此填写回复内容" type="textarea" :rows="5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改咨询回复对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择问题类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="您的姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="问题描述" prop="question">
          <el-input v-model="form.question" placeholder="请输入问题描述" type="textarea" :rows="3"/>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listconsult, getconsult, delconsult, addconsult, updateconsult, exportconsult , getUndo} from "@/api/business/consult";
import { getRole } from "@/api/business/book";
export default {
  name: "consult",
  data() {
    return {
      undototal:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 咨询回复表格数据
      consultList: [],
      // 弹出层标题
      title: "",
      replyOpen: false,
      // 是否显示弹出层
      open: false,
      // 问题类型字典
      typeOptions: [],
      // 是否回复字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        status: null,
        createBy:null,
      },
      createVisible:false,
      // 表单参数
      form: {
      },
      forn:{
        reply:undefined,
        id:undefined,
        status:undefined,
      },
      // 表单校验
      rules: {
        type: [
          { required: true, trigger: "blur", message: "问题类型不能为空" }
        ],
        customerName: [
          { required: true, trigger: "blur", message: "姓名不能为空" }
        ],
      },
      rulesr: {
        reply: [
          { required: true, trigger: "blur", message: "回复内容不能为空" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("poppy_question_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("poppy_reply_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询咨询回复列表 */
    getList() {
      this.loading = true;
      console.log("参数列表")
      console.log("第一个",this.queryParams)     
       getRole().then(response=>{
        if(response.deptId == 205){
          console.log("用户时",response.userName)
          this.queryParams.createBy = response.userName;
          this.createVisible = true;
          let params={
          pageNum: 1,
          pageSize: 10,
          type: null,
          status: null,
          createBy:this.queryParams.createBy,
        }
        this.loading = true;
        listconsult(params).then(response => {
        this.consultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
          getUndo(params).then(response=>{ 
                   this.undototal = response;
                   console.log(response)
                 })
        }else{
        listconsult(this.queryParams).then(response => {
        this.consultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
 getUndo(this.queryParams).then(response=>{ 
                   this.undototal = response;
                   console.log(response)
                 })
        }
      })
    },
    //查询未回复的条数
    getT(){
      getUndo(this.queryParams).then(response=>{
        this.undototal = response;
        console.log(response)
      })
    },
    // 问题类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 是否回复字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.replyOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        customerName: null,
        question: null,
        reply: null,
        tel: null,
        status: "0",
        parentId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getconsult(id).then(response => {
        this.form = response.data;
        this.forn = response.data;
        this.replyOpen = true;
      });
    },
    //回复
    submit(){
      this.forn.status = 1;
      this.$refs["forn"].validate(valid => {
        if (valid) {
          updateconsult(this.forn).then(response=>{
            this.replyOpen = false;
            this.msgSuccess("回复消息成功")
            this.getList();
          })
        }})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateconsult(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.status = 0
            addconsult(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
       }
       });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除咨询回复编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delconsult(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有咨询回复数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportconsult(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
